<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>vue状态管理之vuex（十六） | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Vue,">
  

  <meta name="description" content="了解vuex核心概念请移步 https://vuex.vuejs.org/zh/   一、初始vuex1.1 vuex是什么  vuex 就是把需要共享的变量全部存储在一个对象里面，然后将这个对象放在顶层组件中供其他组件使用 父子组件通信时，我们通常会采用 props + emit 这种方式。但当通信双方不是父子组件甚至压根不存在相关联系，或者一个状态需要共享给多个组件时，就会非常麻烦，数据也会">
<meta name="keywords" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue状态管理之vuex（十六）">
<meta property="og:url" content="http://blog.poetries.top/2018/08/29/vue-vuex/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="了解vuex核心概念请移步 https://vuex.vuejs.org/zh/   一、初始vuex1.1 vuex是什么  vuex 就是把需要共享的变量全部存储在一个对象里面，然后将这个对象放在顶层组件中供其他组件使用 父子组件通信时，我们通常会采用 props + emit 这种方式。但当通信双方不是父子组件甚至压根不存在相关联系，或者一个状态需要共享给多个组件时，就会非常麻烦，数据也会">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://vuex.vuejs.org/vuex.png">
<meta property="og:updated_time" content="2020-08-15T04:25:31.942Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue状态管理之vuex（十六）">
<meta name="twitter:description" content="了解vuex核心概念请移步 https://vuex.vuejs.org/zh/   一、初始vuex1.1 vuex是什么  vuex 就是把需要共享的变量全部存储在一个对象里面，然后将这个对象放在顶层组件中供其他组件使用 父子组件通信时，我们通常会采用 props + emit 这种方式。但当通信双方不是父子组件甚至压根不存在相关联系，或者一个状态需要共享给多个组件时，就会非常麻烦，数据也会">
<meta name="twitter:image" content="https://vuex.vuejs.org/vuex.png">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、初始vuex"><span class="toc-text">一、初始vuex</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-vuex是什么"><span class="toc-text">1.1 vuex是什么</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-vuex中有什么"><span class="toc-text">1.2 vuex中有什么</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-vue-cli中使用vuex的方式"><span class="toc-text">1.3 vue-cli中使用vuex的方式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、modules"><span class="toc-text">二、modules</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、mutations"><span class="toc-text">三、mutations</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、actions"><span class="toc-text">四、actions</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、getters"><span class="toc-text">五、getters</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、mapState、mapGetters、mapActions"><span class="toc-text">六、mapState、mapGetters、mapActions</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-vue-vuex" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">vue状态管理之vuex（十六）</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.08.29</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 2.3k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 10分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <blockquote>
<p>了解vuex核心概念请移步 <a href="https://vuex.vuejs.org/zh/" target="_blank" rel="noopener">https://vuex.vuejs.org/zh/</a></p>
</blockquote>
<p><img src="https://vuex.vuejs.org/vuex.png" alt></p>
<h2 id="一、初始vuex"><a href="#一、初始vuex" class="headerlink" title="一、初始vuex"></a>一、初始vuex</h2><h3 id="1-1-vuex是什么"><a href="#1-1-vuex是什么" class="headerlink" title="1.1 vuex是什么"></a>1.1 vuex是什么</h3><blockquote>
<ul>
<li><code>vuex</code> 就是把需要共享的变量全部存储在一个对象里面，然后将这个对象放在顶层组件中供其他组件使用<ul>
<li>父子组件通信时，我们通常会采用 <code>props + emit</code> 这种方式。但当通信双方不是父子组件甚至压根不存在相关联系，或者一个状态需要共享给多个组件时，就会非常麻烦，数据也会相当难维护</li>
</ul>
</li>
</ul>
</blockquote>
<h3 id="1-2-vuex中有什么"><a href="#1-2-vuex中有什么" class="headerlink" title="1.2 vuex中有什么"></a>1.2 vuex中有什么</h3><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    state: &#123;</span><br><span class="line">        name: <span class="string">'weish'</span>,</span><br><span class="line">        age: <span class="number">22</span></span><br><span class="line">    &#125;,</span><br><span class="line">    getters: &#123;</span><br><span class="line">        personInfo(state) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">`My name is <span class="subst">$&#123;state.name&#125;</span>, I am <span class="subst">$&#123;state.age&#125;</span>`</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    mutations: &#123;</span><br><span class="line">        SET_AGE(state, age) &#123;</span><br><span class="line">            commit(age, age);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    actions: &#123;</span><br><span class="line">        nameAsyn(&#123;commit&#125;) &#123;</span><br><span class="line">            setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                commit(<span class="string">'SET_AGE'</span>, <span class="number">18</span>);</span><br><span class="line">            &#125;, <span class="number">1000</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    modules: &#123;</span><br><span class="line">        a: modulesA</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>个就是最基本也是完整的<code>vuex</code>代码；<code>vuex</code> 包含有五个基本的对象</p>
</blockquote>
<ul>
<li><code>state</code>：存储状态。也就是变量；</li>
<li><code>getters</code>：派生状态。也就是<code>set</code>、<code>get</code>中的<code>get</code>，有两个可选参数：<code>state</code>、<code>getters</code>分别可以获取<code>state</code>中的变量和其他的<code>getters</code>。外部调用方式：<code>store.getters.personInfo()</code>。就和<code>vue</code>的<code>computed</code>差不多；</li>
<li><code>mutations</code>：提交状态修改。也就是<code>set</code>、<code>get</code>中的<code>set</code>，这是<code>vuex</code>中唯一修改<code>state</code>的方式，但不支持异步操作。第一个参数默认是<code>state</code>。外部调用方式：<code>store.commit(&#39;SET_AGE&#39;, 18)</code>。和<code>vue</code>中的<code>methods</code>类似。</li>
<li><code>actions</code>：和<code>mutations</code>类似。不过<code>actions</code>支持异步操作。第一个参数默认是和<code>store</code>具有相同参数属性的对象。外部调用方式：<code>store.dispatch(&#39;nameAsyn&#39;)</code>。</li>
<li><code>modules</code>：<code>store</code>的子模块，内容就相当于是<code>store</code>的一个实例。调用方式和前面介绍的相似，只是要加上当前子模块名，如：<code>store.a.getters.xxx()</code></li>
</ul>
<h3 id="1-3-vue-cli中使用vuex的方式"><a href="#1-3-vue-cli中使用vuex的方式" class="headerlink" title="1.3 vue-cli中使用vuex的方式"></a>1.3 vue-cli中使用vuex的方式</h3><p><strong>目录结构</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">├── index.html</span><br><span class="line">├── main.js</span><br><span class="line">├── components</span><br><span class="line">└── store</span><br><span class="line">    ├── index.js          # 我们组装模块并导出 store 的地方</span><br><span class="line">    ├── state.js          # 跟级别的 state</span><br><span class="line">    ├── getters.js        # 跟级别的 getter</span><br><span class="line">    ├── mutation-types.js # 根级别的mutations名称（官方推荐mutions方法名使用大写）</span><br><span class="line">    ├── mutations.js      # 根级别的 mutation</span><br><span class="line">    ├── actions.js        # 根级别的 action</span><br><span class="line">    └── modules</span><br><span class="line">        ├── m1.js         # 模块1</span><br><span class="line">        └── m2.js         # 模块2</span><br></pre></td></tr></table></figure>
<p><strong>state示例</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> state = &#123;</span><br><span class="line">    name: <span class="string">'weish'</span>,</span><br><span class="line">    age: <span class="number">22</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> state;</span><br></pre></td></tr></table></figure>
<p><strong>getter示例</strong></p>
<blockquote>
<p><code>getters.js</code>示例（我们一般使用<code>getters</code>来获取<code>state</code>的状态，而不是直接使用<code>state</code>）</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> name = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> state.name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> age = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> state.age</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> other = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`My name is <span class="subst">$&#123;state.name&#125;</span>, I am <span class="subst">$&#123;state.age&#125;</span>.`</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>mutation-type示例</strong></p>
<blockquote>
<p>将所有<code>mutations</code>的函数名放在这个文件里</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">export const SET_NAME = &apos;SET_NAME&apos;;</span><br><span class="line">export const SET_AGE = &apos;SET_AGE&apos;;</span><br></pre></td></tr></table></figure>
<p><strong>mutations示例</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> types <span class="keyword">from</span> <span class="string">'./mutation-type.js'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    [types.SET_NAME](state, name) &#123;</span><br><span class="line">        state.name = name;</span><br><span class="line">    &#125;,</span><br><span class="line">    [types.SET_AGE](state, age) &#123;</span><br><span class="line">        state.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong>actions示例</strong></p>
<blockquote>
<p>异步操作、多个<code>commit</code>时</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> types <span class="keyword">from</span> <span class="string">'./mutation-type.js'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    nameAsyn(&#123;commit&#125;, &#123;age, name&#125;) &#123;</span><br><span class="line">        commit(types.SET_NAME, name);</span><br><span class="line">        commit(types.SET_AGE, age);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>modules–m1.js示例</strong></p>
<blockquote>
<p>如果不是很复杂的应用，一般来讲是不会分模块的</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    state: &#123;&#125;,</span><br><span class="line">    getters: &#123;&#125;,</span><br><span class="line">    mutations: &#123;&#125;,</span><br><span class="line">    actions: &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>index.js示例（组装vuex)</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> vue <span class="keyword">from</span> <span class="string">'vue'</span>;</span><br><span class="line"><span class="keyword">import</span> vuex <span class="keyword">from</span> <span class="string">'vuex'</span>;</span><br><span class="line"><span class="keyword">import</span> state <span class="keyword">from</span> <span class="string">'./state.js'</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> getters <span class="keyword">from</span> <span class="string">'./getters.js'</span>;</span><br><span class="line"><span class="keyword">import</span> mutations <span class="keyword">from</span> <span class="string">'./mutations.js'</span>;</span><br><span class="line"><span class="keyword">import</span> actions <span class="keyword">from</span> <span class="string">'./actions.js'</span>;</span><br><span class="line"><span class="keyword">import</span> m1 <span class="keyword">from</span> <span class="string">'./modules/m1.js'</span>;</span><br><span class="line"><span class="keyword">import</span> m2 <span class="keyword">from</span> <span class="string">'./modules/m2.js'</span>;</span><br><span class="line"><span class="keyword">import</span> createLogger <span class="keyword">from</span> <span class="string">'vuex/dist/logger'</span>; <span class="comment">// 修改日志</span></span><br><span class="line"></span><br><span class="line">vue.use(vuex);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> debug = process.env.NODE_ENV !== <span class="string">'production'</span>; <span class="comment">// 开发环境中为true，否则为false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> vuex.Store(&#123;</span><br><span class="line">    state,</span><br><span class="line">    getters,</span><br><span class="line">    mutations,</span><br><span class="line">    actions,</span><br><span class="line">    modules: &#123;</span><br><span class="line">        m1,</span><br><span class="line">        m2</span><br><span class="line">    &#125;,</span><br><span class="line">    plugins: debug ? [createLogger()] : [] <span class="comment">// 开发环境下显示vuex的状态修改</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>最后将<code>store</code>实例挂载到<code>main.js</code>里面的<code>vue</code>上去就行了</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store/index.js'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  el: <span class="string">'#app'</span>,</span><br><span class="line">  store,</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在<code>vue</code>组件中使用时，我们通常会使用<code>mapGetters</code>、<code>mapActions</code>、<code>mapMutations</code>，然后就可以按照<code>vue</code>调用<code>methods</code>和<code>computed</code>的方式去调用这些变量或函数，示例如</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;mapGetters, mapMutations, mapActions&#125; <span class="keyword">from</span> <span class="string">'vuex'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 只写组件中的script部分 */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    computed: &#123;</span><br><span class="line">        ...mapGetters([</span><br><span class="line">            name,</span><br><span class="line">            age</span><br><span class="line">        ])</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">        ...mapMutations(&#123;</span><br><span class="line">            setName: <span class="string">'SET_NAME'</span>,</span><br><span class="line">            setAge: <span class="string">'SET_AGE'</span></span><br><span class="line">        &#125;),</span><br><span class="line">        ...mapActions([</span><br><span class="line">            nameAsyn</span><br><span class="line">        ])</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="二、modules"><a href="#二、modules" class="headerlink" title="二、modules"></a>二、modules</h2><blockquote>
<p>在 src 目录下 , 新建一个 store 文件夹 , 然后在里面新建一个 index.js</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line">Vue.use(vuex);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> vuex.Store(&#123;</span><br><span class="line">    state:&#123;</span><br><span class="line">        show:<span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在 <code>main.js</code> 里的代码应该改成,在实例化 <code>Vue</code>对象时加入 <code>store</code> 对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//vuex</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  el: <span class="string">'#app'</span>,</span><br><span class="line">  router,</span><br><span class="line">  store,<span class="comment">//使用store</span></span><br><span class="line">  template: <span class="string">'&lt;App/&gt;'</span>,</span><br><span class="line">  components: &#123; App &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样就把 <code>store</code> 分离出去了 , 那么还有一个问题是 : 这里 <code>$store.state.show</code> 无论哪个组件都可以使用 , 那组件多了之后 , 状态也多了 , 这么多状态都堆在 store 文件夹下的 <code>index.js</code> 不好维护怎么办 ?</p>
</blockquote>
<ul>
<li>我们可以使用 <code>vuex</code> 的 <code>modules</code> , 把 <code>store</code> 文件夹下的 <code>index.js</code> 改成</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line">Vue.use(vuex);</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> dialog_store <span class="keyword">from</span> <span class="string">'../components/dialog_store.js'</span>;<span class="comment">//引入某个store对象</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> vuex.Store(&#123;</span><br><span class="line">    modules: &#123;</span><br><span class="line">        dialog: dialog_store</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这里我们引用了一个 <code>dialog_store.js</code> , 在这个 <code>js</code>文件里我们就可以单独写 <code>dialog</code> 组件的状态了 </p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    state:&#123;</span><br><span class="line">        show:<span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>做出这样的修改之后 , 我们将之前我们使用的 <code>$store.state.show</code> 统统改为 <code>$store.state.dialog.show</code> 即可</p>
</blockquote>
<ul>
<li>如果还有其他的组件需要使用 <code>vuex</code> , 就新建一个对应的状态文件 , 然后将他们加入 <code>store</code>文件夹下的 <code>index.js</code>文件中的 <code>modules</code> 中</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">modules: &#123;</span><br><span class="line">    dialog: dialog_store,</span><br><span class="line">    other: other,<span class="comment">//其他组件</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="三、mutations"><a href="#三、mutations" class="headerlink" title="三、mutations"></a>三、mutations</h2><blockquote>
<p>对<code>vuex</code> 的依赖仅仅只有一个 <code>$store.state.dialog.show</code> 一个状态 , 但是如果我们要进行一个操作 , 需要依赖很多很多个状态 , 那管理起来又麻烦了</p>
</blockquote>
<ul>
<li><code>mutations</code>里的操作必须是同步的</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    state:&#123;<span class="comment">//state</span></span><br><span class="line">        show:<span class="literal">false</span></span><br><span class="line">    &#125;,</span><br><span class="line">    mutations:&#123;</span><br><span class="line">        switch_dialog(state)&#123;<span class="comment">//这里的state对应着上面这个state</span></span><br><span class="line">            state.show = state.show?<span class="literal">false</span>:<span class="literal">true</span>;</span><br><span class="line">            <span class="comment">//你还可以在这里执行其他的操作改变state</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用 <code>mutations</code> 后 , 原先我们的父组件可以改为 </p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"javascript:;"</span> @<span class="attr">click</span>=<span class="string">"$store.commit('switch_dialog')"</span>&gt;</span>点击<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">t-dialog</span>&gt;</span><span class="tag">&lt;/<span class="name">t-dialog</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> dialog <span class="keyword">from</span> <span class="string">'./components/dialog.vue'</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">  components:&#123;</span><br><span class="line"><span class="javascript">    <span class="string">"t-dialog"</span>:dialog</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用 <code>$store.commit(&#39;switch_dialog&#39;)</code> 来触发 <code>mutations</code> 中的 <code>switch_dialog</code> 方法</p>
</blockquote>
<h2 id="四、actions"><a href="#四、actions" class="headerlink" title="四、actions"></a>四、actions</h2><blockquote>
<p>多个 <code>state</code> 的操作 , 使用 <code>mutations</code>会来触发会比较好维护 , 那么需要执行多个 <code>mutations</code> 就需要用 <code>action</code> 了</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    state:&#123;<span class="comment">//state</span></span><br><span class="line">        show:<span class="literal">false</span></span><br><span class="line">    &#125;,</span><br><span class="line">    mutations:&#123;</span><br><span class="line">        switch_dialog(state)&#123;<span class="comment">//这里的state对应着上面这个state</span></span><br><span class="line">            state.show = state.show?<span class="literal">false</span>:<span class="literal">true</span>;</span><br><span class="line">            <span class="comment">//你还可以在这里执行其他的操作改变state</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    actions:&#123;</span><br><span class="line">        switch_dialog(context)&#123;<span class="comment">//这里的context和我们使用的$store拥有相同的对象和方法</span></span><br><span class="line">            context.commit(<span class="string">'switch_dialog'</span>);</span><br><span class="line">            <span class="comment">//你还可以在这里触发其他的mutations方法</span></span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>那么 , 在之前的父组件中 , 我们需要做修改 , 来触发 <code>action</code> 里的 <code>switch_dialog</code> 方法</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"javascript:;"</span> @<span class="attr">click</span>=<span class="string">"$store.dispatch('switch_dialog')"</span>&gt;</span>点击<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">t-dialog</span>&gt;</span><span class="tag">&lt;/<span class="name">t-dialog</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> dialog <span class="keyword">from</span> <span class="string">'./components/dialog.vue'</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">  components:&#123;</span><br><span class="line"><span class="javascript">    <span class="string">"t-dialog"</span>:dialog</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>使用 <code>$store.dispatch(&#39;switch_dialog&#39;)</code> 来触发 <code>action</code> 中的 <code>switch_dialog</code> 方法。</li>
<li>官方推荐 , 将异步操作放在 <code>action</code>中</li>
</ul>
<h2 id="五、getters"><a href="#五、getters" class="headerlink" title="五、getters"></a>五、getters</h2><blockquote>
<p><code>getters</code>和 <code>vue</code> 中的<code>computed</code> 类似 , 都是用来计算 <code>state</code> 然后生成新的数据 ( 状态 ) 的</p>
</blockquote>
<ul>
<li>假如我们需要一个与状态 <code>show</code> 刚好相反的状态 , 使用 <code>vue</code> 中的 <code>computed</code> 可以这样算出来</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">computed()&#123;</span><br><span class="line">    not_show()&#123;</span><br><span class="line">        <span class="keyword">return</span> !<span class="keyword">this</span>.$store.state.dialog.show;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>那么 , 如果很多很多个组件中都需要用到这个与 <code>show</code>刚好相反的状态 , 那么我们需要写很多很多个 <code>not_show</code>, 使用 <code>getters</code>就可以解决这种问题 </p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    state:&#123;<span class="comment">//state</span></span><br><span class="line">        show:<span class="literal">false</span></span><br><span class="line">    &#125;,</span><br><span class="line">    getters:&#123;</span><br><span class="line">        not_show(state)&#123;<span class="comment">//这里的state对应着上面这个state</span></span><br><span class="line">            <span class="keyword">return</span> !state.show;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    mutations:&#123;</span><br><span class="line">        switch_dialog(state)&#123;<span class="comment">//这里的state对应着上面这个state</span></span><br><span class="line">            state.show = state.show?<span class="literal">false</span>:<span class="literal">true</span>;</span><br><span class="line">            <span class="comment">//你还可以在这里执行其他的操作改变state</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    actions:&#123;</span><br><span class="line">        switch_dialog(context)&#123;<span class="comment">//这里的context和我们使用的$store拥有相同的对象和方法</span></span><br><span class="line">            context.commit(<span class="string">'switch_dialog'</span>);</span><br><span class="line">            <span class="comment">//你还可以在这里触发其他的mutations方法</span></span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>我们在组件中使用 <code>$store.state.dialog.show</code> 来获得状态 <code>show</code> , 类似的 , 我们可以使用 <code>$store.getters.not_show</code> 来获得状态 <code>not_show</code></p>
</blockquote>
<ul>
<li>注意 : <code>$store.getters.not_show</code> 的值是不能直接修改的 , 需要对应的 <code>state</code> 发生变化才能修改</li>
</ul>
<h2 id="六、mapState、mapGetters、mapActions"><a href="#六、mapState、mapGetters、mapActions" class="headerlink" title="六、mapState、mapGetters、mapActions"></a>六、mapState、mapGetters、mapActions</h2><blockquote>
<p>很多时候 , <code>$store.state.dialog.show</code> 、<code>$store.dispatch(&#39;switch_dialog&#39;)</code> 这种写法很不方便</p>
</blockquote>
<ul>
<li>使用 <code>mapState</code>、<code>mapGetters</code>、<code>mapActions</code> 就不会这么复杂了</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-dialog</span> <span class="attr">:visible.sync</span>=<span class="string">"show"</span>&gt;</span><span class="tag">&lt;/<span class="name">el-dialog</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123;mapState&#125; <span class="keyword">from</span> <span class="string">'vuex'</span>;</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">  computed:&#123;</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="comment">//这里的三点叫做 : 扩展运算符</span></span></span><br><span class="line">    ...mapState(&#123;</span><br><span class="line"><span class="javascript">      show:<span class="function"><span class="params">state</span>=&gt;</span>state.dialog.show</span></span><br><span class="line">    &#125;),</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>相当于</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-dialog</span> <span class="attr">:visible.sync</span>=<span class="string">"show"</span>&gt;</span><span class="tag">&lt;/<span class="name">el-dialog</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123;mapState&#125; <span class="keyword">from</span> <span class="string">'vuex'</span>;</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">  computed:&#123;</span><br><span class="line">    show()&#123;</span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> <span class="keyword">this</span>.$store.state.dialog.show;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>mapGetters</code>、<code>mapActions</code> 和 <code>mapState</code> 类似 , <code>mapGetters</code> 一般也写在 <code>computed</code> 中 , <code>mapActions</code> 一般写在 <code>methods</code> 中</p>
</blockquote>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/08/28/vue-animation-transition/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/08/29/redux-saga-and-redux-thunk/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
